package guo.dyn;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CglibDyn {
    private Object target;

    public CglibDyn(Object target){
        this.target = target;
    }

    public Object getProxy(){

        Class<?> aClass = target.getClass();
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("cglib start");
                Object o1 = methodProxy.invokeSuper(o, objects);
                System.out.println("cglib end");
                return o1;
            }
        });
        return enhancer.create();
    }
}
